package com.sens.alipay.utils;

import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;

import com.sens.alipay.Configure;
import com.sens.common.TimeUtil;
import com.sens.tables.AlipayOrder;
import com.sens.vo.AliPayOrderVo;

import java.util.Date;

public class OrderUtil {
    public static AlipayOrder createAlipayOrder(AliPayOrderVo aliPayOrderVo) {
        if (aliPayOrderVo == null) return null;
        AlipayOrder alipayOrder = new AlipayOrder();
        alipayOrder.setTime(aliPayOrderVo.getTime());
        alipayOrder.setTradeNo(aliPayOrderVo.getTradeNo());
        alipayOrder.setMark(aliPayOrderVo.getMark());
        alipayOrder.setMarkType(aliPayOrderVo.getMarkType());
        alipayOrder.setMoney(aliPayOrderVo.getMoney());
        alipayOrder.setUnit(aliPayOrderVo.getUnit());
        alipayOrder.setPayType(aliPayOrderVo.getPayType());
        alipayOrder.setState(aliPayOrderVo.getState());
        alipayOrder.setUserId(aliPayOrderVo.getUserId());
        alipayOrder.setOrderType("支付宝");
        alipayOrder.setHasUpload(false);
        return alipayOrder;
    }

    public static CharSequence getShowMark(AlipayOrder alipayOrder) {
        String markType = alipayOrder.getMarkType();
        int index = -1;
        for (int i = 0; i < Configure.monitors.length; i++) {
            if (Configure.monitors[i].equals(markType)) {
                markType = Configure.monitorNames[i] + "-" + markType;
                index = i;
                break;
            }
        }
        SpannableString ss = new SpannableString(markType + alipayOrder.getMark());
        if (index != -1) {
            ss.setSpan(new ForegroundColorSpan(Configure.monitorColors[index]), 0, Configure.monitorNames[index].length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        }
        return ss;
    }

    public static TradeInfoHelper createTestOrder(Class TradeInfoClass, String monitor) throws Throwable {
        Date date = new Date();
        String createTime = TimeUtil.getFormatTime("yyyy-MM-dd HH:mm", date);
        String tradeNo = TimeUtil.getFormatTime("yyyyMMdd", date) + System.currentTimeMillis();
        TradeInfoHelper tradeInfoHelper = new TradeInfoHelper(TradeInfoClass);
        tradeInfoHelper.setBusinessId("PAY_HELPER_CARD_2222222222222222");
        tradeInfoHelper.setContent("{\"content\":\"￥0.01\",\"assistMsg1\":\"二维码收款到账通知\",\"assistMsg2\":\"测试数据\",\"linkName\":\"\",\"buttonLink\":\"\",\"templateId\":\"WALLET-FWC@remindDefaultText\"}");
        tradeInfoHelper.setExtraInfo("{\"actions\":[{\"name\":\"\",\"url\":\"\"},{\"name\":\"查看详情\",\"url\":\"alipays://platformapi/startapp?appId=20000003&actionType=toBillDetails&tradeNO=" + tradeNo + "&bizType=D_TRANSFER\"}],\"ad\":[],\"amountTip\":\"\",\"bizMonitor\":\"{\\\"businessId\\\":\\\"PAY_HELPER_CARD_2222222222222222\\\",\\\"expireLink\\\":\\\"\\\",\\\"gmtCreate\\\":1545547368729,\\\"gmtValid\\\":1548139368723,\\\"hiddenSum\\\":\\\"0\\\",\\\"homePageTitle\\\":\\\"测试助手: ￥0.01 二维码收款到账通知\\\",\\\"icon\\\":\\\"https://gw.alipayobjects.com/zos/rmsportal/EMWIWDsKUkuXYdvKDdaZ.png\\\",\\\"id\\\":\\\"2a013d053abdef80ea3f636c1761bce7568200059_00094_zfzs0012222222222222222\\\",\\\"link\\\":\\\"alipays://platformapi/startapp?appId=20000003&actionType=toBillDetails&tradeNO=" + tradeNo + "&bizType=D_TRANSFER?tagid=MB_SEND_PH\\\",\\\"linkName\\\":\\\"\\\",\\\"msgId\\\":\\\"2a013d053abdef80ea3f636c1761bce75682\\\",\\\"msgType\\\":\\\"NOTICE\\\",\\\"operate\\\":\\\"UPDATE\\\",\\\"status\\\":\\\"\\\",\\\"templateCode\\\":\\\"00059_00094_zfzs001\\\",\\\"templateId\\\":\\\"WALLET-BILL@BLPaymentHelper\\\",\\\"templateName\\\":\\\"测试助手\\\",\\\"templateType\\\":\\\"BN\\\",\\\"title\\\":\\\"测试助手\\\",\\\"userId\\\":\\\"2222222222222222\\\"}\",\"content\":[{\"content\":\"累计收款金额0.01元，累计收款1笔\",\"title\":\"今日汇总：\"},{\"content\":\"佚名\",\"title\":\"付款人：\"},{\"content\":\"测试数据\",\"title\":\"" + monitor + "\"},{\"content\":\"" + createTime + "\",\"title\":\"到账时间：\"}],\"date\":\"12月23日\",\"failTip\":\"\",\"goto\":\"alipays://platformapi/startapp?appId=20000003&actionType=toBillDetails&tradeNO=" + tradeNo + "&bizType=D_TRANSFER\",\"infoTip\":\"\",\"money\":\"0.01\",\"status\":\"二维码收款到账通知\",\"unit\":\"元\"}");
        tradeInfoHelper.setGmtCreate(1545547368729L);
        tradeInfoHelper.setGmtValid(1548139368723L);
        tradeInfoHelper.setHiddenSum("0");
        tradeInfoHelper.setHomePageTitle("测试助手: ￥0.01 二维码收款到账通知");
        tradeInfoHelper.setIcon("https://gw.alipayobjects.com/zos/rmsportal/EMWIWDsKUkuXYdvKDdaZ.png");
        tradeInfoHelper.setId("2a013d053abdef80ea3f636c1761bce7568200059_00094_zfzs0012222222222222222");
        tradeInfoHelper.setLink("alipays://platformapi/startapp?appId=20000003&actionType=toBillDetails&tradeNO=" + tradeNo + "&bizType=D_TRANSFER?tagid=MB_SEND_PH");
        tradeInfoHelper.setMsgId("2a013d053abdef80ea3f636c1761bce75110");
        tradeInfoHelper.setMsgType("NOTICE");
        tradeInfoHelper.setOperate("UPDATE");
        tradeInfoHelper.setTemplateCode("00110_00110_zfzs110");
        tradeInfoHelper.setTemplateId("WALLET-BILL@BLPaymentHelper");
        tradeInfoHelper.setTemplateName("测试助手");
        tradeInfoHelper.setTemplateType("BN");
        tradeInfoHelper.setTitle("测试助手");
        tradeInfoHelper.setUserId("1000100101");
        return tradeInfoHelper;
    }
}
